package com.we.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class AppServer {
    private int port;
    public AppServer(int port){
        this.port=port;
    }
    public void start(){
        //1.创建eventLoop，老板只负责处理请求，之后会将请求分发给worker
        EventLoopGroup boss=new NioEventLoopGroup(2);
        EventLoopGroup worker=new NioEventLoopGroup(10);
        try {
            //2，需要一个服务器引导程序
        ServerBootstrap serverBootstrap=new ServerBootstrap();
        //3.配置服务器
        serverBootstrap = serverBootstrap.group(boss,worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        //添加一个服务器
                        socketChannel.pipeline().addLast(new MyChannelHandler());
                    }
                });
        //4.绑定端口
            //一定要同步，拿到channelFuture
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();

            //同步进行阻塞住了，可以进行处理一些事情。
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            try {
                boss.shutdownGracefully().sync();
                worker.shutdownGracefully().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new AppServer(8080).start();
    }

}
